<?php
namespace app\home\controller;

use app\admin\model\UserModel;
use think\Controller;
use think\Db;
use codes\Qrcode;
/*-------------------------------马鹏程------------------------------------------*/
#用户接口
class Useraccount extends Base
{
    protected $userId;
    public function __construct()
    {
        parent::__construct();
        $this->userId = session('uid');
        $this->users  = new UserModel();
        if (!$this->userId) {
            return json(['status' => 2000, 'message' => '請先登錄', 'data' => '']);
        }

    }

    #充值
    public function recharge()
    {
        $user    = $this->users->field('phone,nickname,pool,activity_pool,type,stack')->where('id', $this->userId)->find();
        if(!is_file('./uploads/qrcode/'.$user['phone'].'.png')){
            $path = ROOT_PATH.'public/uploads/qrcode/'.$user['phone'].'.png';
            Qrcode::png($user['stack'],$path);
        }
        $user['qrcode'] =  DOMAIN.'/uploads/qrcode/'.$user['phone'].'.png';
        return json(['status' => 200, 'message' => '請求成功', 'data' => $user]);
    }
    #充值
    public function rechargeLog()
    {
        // $pagecount = 8;
        // $page      = (int) input('p');
        // $start     = $page > 0 ? ($page - 1) *$pagecount : 0;
        // $data = db('user_account')->where(['type'=>5])->field('create_time,money,balance')->limit($start,$pagecount)->order('id desc')->select();
        $data = db('user_account')->where(['type'=>5])->field('create_time,money,balance')->order('id desc')->select();
        return json(['status' => 200, 'message' => '查询成功', 'data' => $data]);
    }

    #转入
    public function shift_to()
    {
    	$param = input('param.');
    	$user = $this->users->where('id',$this->userId)->find();
    	if($user['two_password'] != md5($param['password'])){
    		return json(['status' => -1, 'message' => '交易密碼錯誤']);
    	}
    	$config = db('config')->find();
    	if($config['account_little_money'] > $param['money']){
    		return json(['status' => -1, 'message' => '轉賬金額不得小於'.$config['account_little_money']]);
    	}
        if($param['money']>$user['activity_pool']){
            return json(['status' => -1, 'message' => '轉賬金額不得大於您的活動帳戶餘額']);
        }
    	Db::startTrans();
        try {
        	#活動帳戶轉入礦池
        	$this->users->where('id',$this->userId)->setDec('activity_pool',$param['money']);
        	$this->users->where('id',$this->userId)->setInc('pool',$param['money']);
            $balance = $user['pool'] + $param['money'];
            // $balance = $param['money'];
        	$account = [
        		'uid'		  => $this->userId,
				'source_id'	  => $this->userId,	
				'money'		  => $param['money'],
				'balance'	  => $balance,
				'message'	  => '',	
				'type'		  => 2,		
				'status'	  => 2,		
				'create_time' => date('YmdHis')
        	];
        	db('user_account')->insert($account);
        	$pool = [
        		'uid'			=> 	$this->userId,
				'order_sn'		=> 	orderSn(),
				'source_id'		=> 	$this->userId,
				'source_msg'	=> 	'活动账户转入矿池',
				'money'			=> 	$param['money'],
                'balance'       =>  $balance,
				'unlock_money'	=> 	$param['money'],
				'unlock_date'	=> 	date('Y-m-d H:i:s',strtotime('+90 days')),
				'create_time'	=> 	date('YmdHis'),
        	];
        	db('user_pool')->insert($pool);
            $valid_value=db('config')->where(['id'=>1])->value('valid_value'); //最小数值
            if($user['type'] == 1 && $balance >= $valid_value){
                $this->users->where('id',$this->userId)->update(['type'=>2]);
                db('user_position')->where('uid',$this->userId)->update(['is_effective'=>2]);
            }
            // 提交事务
            Db::commit();
            return json(['status' => 200, 'message' => '轉賬成功']);;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['status' => -1, 'message' => '轉賬失敗']);;
        }
    }
    #轉入記錄
    public function shift_to_log()
    {

        $start_time=input('param.start_time');
        $end_time=input('param.end_time');
        $wheres=[];
        if($start_time && $end_time){
            $wheres['create_time']=['between',[$start_time,$end_time]];
        }
        $pagecount = 10;
        $page      = (int) input('p');
        $start     = $page > 0 ? ($page - 1) *$pagecount : 0;
    	$data = db('user_pool')->where(['uid'=>$this->userId,'source_id'=>$this->userId])->where($wheres)->field('money,balance,create_time,unlock_date')->limit($start,$pagecount)->order('id desc')->select();
    	return json(['status' => 200, 'message' => '查詢成功','data'=>$data]);
    }

    #解锁记录
    public function unlock_log()
    {
        $pagecount = 10;
        $page      = (int) input('p');
        $start     = $page > 0 ? ($page - 1) *$pagecount : 0;
        $data = db('unlock')->where(['uid'=>$this->userId])->field('money,balance,create_time')->limit($start,$pagecount)->order('id desc')->select();
        return json(['status' => 200, 'message' => '查詢成功','data'=>$data]);
    }
    #转出记录
    public function shift_out_log()
    {
        $pagecount = 10;
        $page      = (int) input('p');
        $start     = $page > 0 ? ($page - 1) *$pagecount : 0;
        $data = db('user_account')->where(['uid'=>$this->userId,'source_id'=>$this->userId,'type'=>2,'class'=>2])->field('money,balance,create_time')->limit($start,$pagecount)->order('id desc')->select();
        return json(['status' => 200, 'message' => '查詢成功','data'=>$data]);
    }

}
